<template>
  <div class="app-container">
    <div class="search">
      <p class="float-left mr-10 mb-10" style="width: 200px">
        <el-input v-model="searchParams.name" placeholder="输入名称"></el-input>
      </p>
      <p class="float-left mr-10 mb-10">
        <el-button type="primary" @click="searchStore">搜索</el-button>
        <el-button type="primary" @click="resetStore">重置</el-button>
      </p>
    </div>
    <div class="my-10">
      <bdButton :handlerBtn="handlerBtn"/>
    </div>
    <bdTable :tableHeader="tableHeader" :tableData="tableData" class="mb-10" />
    <bd-page
      :pageSize="pages"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></bd-page>
    <addStoreAndEditStore ref="rulesInfo" @update:visible="searchStore(true)"/>
  </div>
</template>
<script>
import { jfcsPage, jfcsDelete } from '@/api/jfcs'
import addStoreAndEditStore from './addStoreAndEditStore'
import { getImgUrl } from '@/filters/filters.js'

export default {
  name: '',
  components: {
    addStoreAndEditStore
  },
  data () {
    return {
      searchParams: {
        name: null
      },
      handlerBtn: [
        {
          id: 1,
          name: '新增',
          handler: this.addStore
        }
      ],
      tableHeader: {
        type: 'selection',
        header: [
          {
            prop: 'name',
            label: '商品名称'
          },
          {
            prop: 'picUrl',
            label: '商品封面',
            type: 'img',
            previewSrcList: (row) => {
              return [getImgUrl(row.picUrl)]
            }
          },
          {
            prop: 'price',
            label: '市场价'
          },
          {
            prop: 'jfPrice',
            label: '积分价格'
          },
          {
            prop: 'createTime',
            label: '创建时间'
          },
          {
            label: '操作',
            type: 'button',
            minWidth: 150,
            btn: [
              {
                id: 1,
                name: '编辑',
                handler: this.handleEdit
              },
              {
                id: 2,
                name: '删除',
                type: 'danger',
                handler: this.handleDelete
              }
            ]
          }
        ]
      },
      // 列表数据
      tableData: [],
      // 分页
      pages: {
        page: 1,
        pageSize: 10
      },
      total: 0
    }
  },
  // 计算属性
  computed: {},
  watch: {},
  mounted () {
    this.searchStore()
  },
  methods: {
    async searchStore (clearPage = false) {
      if (clearPage) {
        this.pages.page = 1
        this.pages.pageSize = 10
      }
      const params = {
        ...this.pages,
        ...this.searchParams
      }
      const { total, list } = await jfcsPage(params)
      this.total = total
      this.tableData = list
    },
    // 重置
    resetStore () {
      this.searchParams.name = ''
      this.searchStore(true)
    },
    // 新增
    addStore () {
      this.$refs.rulesInfo.isShow()
    },
    // 修改
    handleEdit (e, row) {
      this.$refs.rulesInfo.isShow(row, true)
    },
    // 删除
    handleDelete (e, row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await jfcsDelete({ id: row.id })
        this.searchStore()
      }).catch(() => {
      })
    },
    // 条数
    handleSizeChange (val) {
      this.pages.pageSize = val
      this.searchStore()
    },
    // 页数
    handleCurrentChange (val) {
      this.pages.page = val
      this.searchStore()
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
